package com.zy.qudadid.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zy.lcpassenger.R;
import com.zy.qudadid.model.Shop;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.presenter.ShopPresenter;
import com.zy.qudadid.ui.adapter.base.BaseAdapter;
import com.zy.qudadid.utils.AESCrypt;
import com.zy.qudadid.utils.StringUtil;
import com.zy.qudadid.utils.ToolsUtil;
import com.zy.qudadid.utils.UserUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by liuchao on 2017/4/10.
 */

public class ShopAdapter extends BaseAdapter<ShopAdapter.ViewHolder, Shop, ShopPresenter> {

    int curPosition;

    public ShopAdapter(Context context) {
        super(context);
    }

    @Override
    public int provideItemLayoutId() {
        return R.layout.item_shop;
    }

    @Override
    public ViewHolder createVH(ViewGroup parent, int viewType, View view) {
        return new ViewHolder(view);
    }

    @Override
    public void bindData(ViewHolder holder, int position) {
        Shop item = data.get(position);

        holder.txtDate.setText("有效期至" + item.getEndtime());
        holder.txtDisCount.setText(item.getRebate());
        if(item.getTexi().equals("0")){
            if(item.getPricar().equals("0")){
                if(item.getSpecar().equals("0")){

                }else{
                    holder.txtName.setText("专车券");
                    holder.txtName2.setText("专车专用券");
                    holder.txtName3.setText("专享专车折扣");
                }
            }else{
                holder.txtName.setText("网约车券");
                holder.txtName2.setText("网约车专用券");
                holder.txtName3.setText("专享网约车折扣");
            }
        }else{
            holder.txtName.setText("出租车券");
            holder.txtName2.setText("出租车专用券");
            holder.txtName3.setText("专享出租车折扣");
        }

        holder.txtPrice.setText(item.getNum());


        holder.txtIntegral.setText("积分："+item.getAccount());

        holder.txtConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                curPosition = position;
//                Toast.makeText(context,curPosition+"",Toast.LENGTH_SHORT).show();

                Map<String,String> map = new HashMap<String, String>();
                map.put("coupon_id",data.get(curPosition).getCoupon_id());
                map.put("user_id",new UserUtil(context).getUserId());
                map.put("num",data.get(curPosition).getAccount());
                String json = StringUtil.toJson(map);
                try {
                    String data1 = AESCrypt.getInstance().encrypt(json);
                    String d = StringUtil.replaceBlank(data1);
                    OkHttpUtils.post()
                            .url(Const.BASE_URL+"api.php/Shop/exchanges")
                            .addParams("args",d)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int i) {
                                    ToolsUtil.print("----","领取优惠券失败："+e.getMessage().toString());
                                }

                                @Override
                                public void onResponse(String s, int i) {
                                    JSONObject json;
                                    try {
                                        json = new JSONObject(s);
                                        int code = json.getInt("code");
                                        if(code == Const.OK){
                                            Toast.makeText(context,"领取成功",Toast.LENGTH_SHORT).show();
                                        }else{
                                            Toast.makeText(context,json.getString("message"),Toast.LENGTH_SHORT).show();
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                } catch (Exception ex) {
                }

            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @Nullable
        @Bind(R.id.txtDisCount)
        TextView txtDisCount;
        @Nullable
        @Bind(R.id.txtName)
        TextView txtName;
        @Nullable
        @Bind(R.id.txtDate)
        TextView txtDate;
        @Nullable
        @Bind(R.id.txtPrice)
        TextView txtPrice;
        @Nullable
        @Bind(R.id.txtConfirm)
        TextView txtConfirm;
        @Nullable
        @Bind(R.id.txtName2)
        TextView txtName2;
        @Nullable
        @Bind(R.id.txtName3)
        TextView txtName3;
        @Nullable
        @Bind(R.id.txtIntegral)
        TextView txtIntegral;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
